#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT    6100
#define SERVER_IP   "172.16.113.104"
int main()
{
        int s;
        int size=0,write_size=0,recv_size=0;
        struct sockaddr_in addr;
        char buffer[4096*2];
        FILE *fp=fopen("test.h264","w+");
        if ((s=socket(AF_INET,SOCK_STREAM,0))<0) {
                perror("socket");
                exit(1);
        }
        /*填写sockaddr_in结构*/
        memset(&addr,0,sizeof(addr));
        addr.sin_family=AF_INET;
        addr.sin_port=htons(PORT);
        addr.sin_addr.s_addr=inet_addr(SERVER_IP);
        /*尝试连接*/
        if(connect(s,(struct sockaddr*)&addr,sizeof(addr))<0) {
                perror("connect");
                exit(1);
        }
        /* 接收由server端传来的信息*/
        // recv(s,buffer,sizeof(buffer),0);
        //  printf("%s\n",buffer);
        while(1) {
                bzero(buffer,sizeof(buffer));
                /*从标准输入取得字符串*/

                /*将字符串传送给sever端*/

                size=recv(s,buffer,sizeof(buffer),0);
                if(size<0) {
                        perror("send");
                        exit(1);
                }

                printf("接受数据%d\n",size);
                write_size=fwrite(buffer,1,size,fp);
                printf("写入数据%d\n",write_size);
                fflush(fp);
        }

        return 0;
}
